﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Vara_Data_Layer.Vara_Data_Additional_Option
{
    public class Vara_Data_Additional_Option_Controller : Vara_Data_IAdditional_Option_Actions
    {
        private List<Vara_Data_IAdditional_Option> _AdditionalOptions;
        public List<Vara_Data_IAdditional_Option> AdditionalOptions
        {
            get
            {
                if (_AdditionalOptions == null)
                    Refresh_AdditionalOptions_List();

                return _AdditionalOptions;
            }
        }

        public Vara_Data_IAdditional_Option ReturnAdditionalOption(int ID)
        {
            Vara_Data_IAdditional_Option AdditionalOptionToReturn;
            AdditionalOptionToReturn = AdditionalOptions.Find(delegate(Vara_Data_IAdditional_Option AdditionalOption) { return AdditionalOption.ID == ID; });
            return AdditionalOptionToReturn;
        }

        public void Refresh_AdditionalOptions_List()
        {
            DataTable DataTableAdditionalOptions;
            using (SqlEngine NewSqlConnector = new SqlEngine("AssetTracking"))
            {
                DataTableAdditionalOptions = NewSqlConnector.ExecuteProcedure("Return_Additional_Options", new SqlEngineProcedureVariable("@Username", XentrixModules.Reusable.Username()));
            }
            List<Vara_Data_IAdditional_Option> ListOfAdditionalOptions = new List<Vara_Data_IAdditional_Option>();
            int intRowCounter = 0;
            foreach (DataRow R in DataTableAdditionalOptions.Rows)
            {
                Vara_Data_IAdditional_Option AdditionalOptionToAppend = new Vara_Data_Additional_Option(int.Parse(R["ID"].ToString()), bool.Parse(R["ReadOnly"].ToString()), intRowCounter);
                AdditionalOptionToAppend.Name = R["Name"].ToString();
                ListOfAdditionalOptions.Add(AdditionalOptionToAppend);
                intRowCounter++;
            }
            _AdditionalOptions = ListOfAdditionalOptions;
        }
    }
}
